package com.wac.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.blog.pojo.Question;
import com.wac.blog.pojo.QuestionCategory;
import com.wac.blog.service.QuestionCategoryService;
import com.wac.blog.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wac
 * @since 2020-08-04
 */
@Controller
@RequestMapping("/questioncategory")
public class QuestionCategoryController {
    @Autowired
    QuestionCategoryService questionCategoryService;

    @Autowired
    QuestionService questionService;

    @RequestMapping("/{cid}")
    public String questioncategory(@PathVariable Integer cid, Model model){
        Page<Question> page = new Page<>(1,10);
        questionService.page(page, new QueryWrapper<Question>().eq("category_id", cid).orderByDesc("gmt_create"));
        List<Question> questionList = page.getRecords();
        model.addAttribute("questionList", questionList);
        model.addAttribute("page",page );
        List<QuestionCategory> questionCategoryList = questionCategoryService.list(null);
        model.addAttribute("questionCategoryList",questionCategoryList );
        QuestionCategory category = questionCategoryService.getById(cid);
        model.addAttribute("thisCategoryName", category.getCategory());
        return "question/list";
    }
}

